<template>
	<view class="tabs-box" v-if="list.length">
		<view v-for="(item,index) in list" :key="index" :class="tabItemClass(index)" @click="current=index">
			{{item}}
		</view>
		<view class="tab-line" :style="tabLineStyle"></view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default () {
					return ['当前周期', '历史周期'];
				}
			},
			value: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				current: 0,
			}
		},
		watch: {
			current(current) {
				this.$emit('change', current);
				this.$emit('input', current);
			}
		},
		computed: {
			tabLineStyle() {
				let width = (100 / this.list.length).toFixed(2)
				let left = width * this.current.toFixed(2);
				return {
					left: left + '%',
					width: width + '%'
				}
			},
		},
		methods: {
			tabItemClass(index) {
				let res = ['tab-item']
				if (this.current == index) {
					res.push('current')
				}
				return res;
			}
		},
		created() {
			this.current = this.value;
		}
	}
</script>

<style lang="scss">
	.tabs-box {
		display: flex;
		background: #fff;
		position: relative;
		padding-bottom: 5rpx;

		.tab-item {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			line-height: 2.5em;
			color: #333;

			&.current {
				color: #168fec;
			}
		}

		.tab-line {
			position: absolute;
			bottom: 0;
			height: 5rpx;
			background: #168fec;
			transition: all .4s;
		}
	}
</style>
